package com.gwtgeo.ui.server.handler;

import java.util.List;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.inject.Inject;
import com.gwtgeo.service.exception.GwtgeoException;
import com.gwtgeo.service.manager.BaseLayersManager;
import com.gwtgeo.service.model.WMSLayerDef;
import com.gwtgeo.ui.server.spring.ActionHandlerBean;
import com.gwtgeo.ui.shared.rpc.GetBaseLayers;
import com.gwtgeo.ui.shared.rpc.GetBaseLayersResult;

@ActionHandlerBean
public class GetBaseLayersHandler implements ActionHandler<GetBaseLayers, GetBaseLayersResult> {

	@Autowired
	private BaseLayersManager baseLayersManager;

	@Inject
	public GetBaseLayersHandler() {
		super();
	}

	@Override
	public GetBaseLayersResult execute(GetBaseLayers action, ExecutionContext context)
			throws ActionException {
		try {
			List<WMSLayerDef> layers = baseLayersManager.getAll();
			return new GetBaseLayersResult(layers);
		} catch (GwtgeoException e) {
			throw new ActionException(e);
		}
	}

	@Override
	public Class<GetBaseLayers> getActionType() {
		return GetBaseLayers.class;
	}

	@Override
	public void rollback(GetBaseLayers arg0, GetBaseLayersResult arg1, ExecutionContext arg2)
			throws ActionException {
		// TODO PMA20100509 Handle failure.
	}

	public void setBaseLayersManager(BaseLayersManager baseLayersManager) {
		this.baseLayersManager = baseLayersManager;
	}
}
